#include"common_headers.h"
int listdir31(char dir_name[300], int flag, char outputfile[500])
{
	//DECLARATIONS
	int length_of_dir_name, breaking_due_to_error, q, is_valid_file_name, is_mounted, is_same_filepath;
    struct narytree *temp,*newadd;
    char current_character, next_character;
    FILE *fptr1;
    
    //INITIALIZATION
    length_of_dir_name = 0;
    breaking_due_to_error = 0;
    q=0;
    is_valid_file_name = 0;
    is_mounted = 0;
    is_same_filepath = -1;
    
    temp=nroot;    
    fptr1=fopen(outputfile,"r");
   
   	//CHECK IF VFS MOUNTED
   	is_mounted = validateMountState();
    if( is_mounted )
    {
    	//CHECK IF FILENAME IS VALID  
    	is_valid_file_name = validateFileName(outputfile);  	
    	if(is_valid_file_name)
    	{
    		//VALIDATE THE FORMAL ARGUMENTS RECEIVED FOR CORRECT FORMAT     		
    		for(q=0; q < length_of_dir_name; q++)
		   	{
		   		current_character = dir_name[q];
		   		next_character = dir_name[q+1];
		   		
		   		//CHECK IF THE DIRECTORY PATH RECEIVED CONTAINS TWO CONSECUTIVE SLASH '//' (NOT ALLOWED)
		   		if(current_character=='/' && next_character=='/')
		   		{
			   		//CONSECUTIVE '/' ARE NOT ALLOWED, HENCE BREAKING DUE TO ERROR
		   			breaking_due_to_error=1;
		   			break;
		   		}		   		
		   	}   	
		   	
		   	//CHECK IF NOT THE INCORRECT INPUT DIRECTORY PATH
		   	if((breaking_due_to_error!=1))
	   		{
	   			//CHECK IF RECURSIVE (or) NON-RECURSIVE FLAG (0: NON-RECURSIVE, 1: RECURSIVE)
		   		if(flag==0 || flag==1)
		   		{			   			   	
					while(1)
					{
						fptr1=fopen(outputfile,"w");
						
						//CHECK IF SAME FILEPATH
						is_same_filepath = strcmp(dir_name,temp->mydes->locationpath);
						if(strcmp(dir_name,temp->mydes->locationpath)==0)
						{
							//CHECK IF CHILD EXIST
							if(temp->child!=NULL)
							{
							    temp=temp->child;
							}
							
							//CHECK IF NON-RECURSIVE
							if(flag==0)
							{
							    int state=nonrecur_nary(temp,fptr1);
							    fclose(fptr1);
							    if(state==1)
							    {
							    	printf("listdir_SUCCESS\n");
							    }
							    return 1;
							}
							
							//CHECK IF RECURSIVE
							else if(flag==1)
							{
							    int state=recur_nary(temp,fptr1);
							    fclose(fptr1);
							    if (state==1)
							    {
						    		printf("listdir_SUCCESS\n");
							    }
							    return 1;
							}
							
							//CHECK IF FLAG IS INVALID
							else
							{
								//INVALID_FLAG
							    printf("listdir_FAILURE %s\n",ERR_VFS_LISTDIR_02);
							    fclose(fptr1);
							    return 0;
							}
						}
						
						//CHECK IS FILEPATH IS A SUBSET, AND SOME PATH EXIST AHEAD
						else if(strncmp(dir_name,temp->mydes->locationpath,strlen(temp->mydes->locationpath))==0 && (temp==nroot || dir_name[strlen(temp->mydes->locationpath)]=='/'))
						{
							//CHECK IF CHILD EXISTS
							if(temp->child==NULL)
							{
								//CANNOT_FIND_SPECIFIED_PATH_OR_DIR
							    printf("listdir_FAILURE %s\n",ERR_VFS_LISTDIR_01);
							    fclose(fptr1);
							    return 0;
							}
							else
							{
							    temp=temp->child;
							}
						}
						
						//CHECK IF RIGHT SIBLING IS NULL
						else if(temp->rightsibling==NULL)
						{
							//CANNOT_FIND_SPECIFIED_PATH_OR_DIR
							printf("listdir_FAILURE %s\n",ERR_VFS_LISTDIR_01);
							fclose(fptr1);
							return 0;
						}
						
						//CHECK IF RIGHT SIBLING IS NULL
						else if(temp->rightsibling!=NULL)
						{
							temp=temp->rightsibling;
						}
					}
					return 1;				
				}
		   		else
		   		{
		   			//INVALID FLAG
		   			printf("listdir_FAILURE %s\n", ERR_VFS_LISTDIR_02);
		   		}
	   		}
	   		else
	   		{
	   			//INVALID_DIR_NAME (1st PARAMETER - FROM WHERE WE NEED TO DISPLAY)
				printf("listdir_FAILURE %s\n", ERR_VFS_LISTDIR_06);
	   		}    		
    	}
    	else
    	{
    		//INVALID FILE NAME
    		printf("listdir_FAILURE %s\n", ERR_VFS_LISTDIR_05);
    	
    }
	}
    else
    {
    	//VFS_NOT_MOUNTED
		printf("listdir_FAILURE %s\n", ERR_VFS_LISTDIR_04);
    }
	return 0;
}

